package com.xd.user.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.user.mapper.PermissionMapper;
import com.xd.user.api.entity.Permission;
import com.xd.user.service.PermissionService;
import com.xd.user.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限(Permission)表服务实现类
 *
 * @author hbx
 * @since 2019-12-30 17:17:23
 */
@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper mapper;

    @Override
    public List<Permission> listByUserId(String id) {
        return mapper.listByUserId(id);
    }

    /**
     * 封装菜单树
     * @return 菜单树
     */
    @Override
    public List<Tree<String>> getTree() {
        List<Permission> list = this.mapper.selectList(null);
        return TreeUtil.build(list, "0", ((object, treeNode) -> treeNode = TreeUtil.createPermissionTreeNode(object, treeNode)));
    }

    @Override
    public List<Tree<String>> getTree(List<Permission> list) {
        return TreeUtil.build(list, "0", ((object, treeNode) -> treeNode = TreeUtil.createPermissionTreeNode(object, treeNode)));
    }


    /**
     * 获取菜单树,设置是否选中
     * @param collect 菜单集合
     * @return 菜单树
     */
    @Override
    public List<Tree<String>> getTree(List<String> collect,List<Permission> list) {
        if(ObjectUtil.isEmpty(collect)){
            return getTree(list);
        }
        return TreeUtil.build(list, "0", ((object, treeNode) -> {
            Tree<String> stringTree = TreeUtil.createPermissionTreeNode(object, treeNode);
            stringTree.putExtra("checked", collect.contains(object.getId()));
        }));
    }


}
